package cn.ha.toy.web;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ha.toy.annotation.CrudRequestMapping;

@Controller
@CrudRequestMapping("crud")
public abstract class AbstractCrudController<T> {
	
	private CrudService<T> crudService;
	/**
	 * create时的页面跳转
	 * @return
	 */
	@CrudRequestMapping("input")
	@ResponseBody
	public String input(){
		System.out.println("input");
		return "input";
	}

	/**
	 * create时的数据保存
	 * 校验需要通过pojo的注解解决
	 * @param model
	 * @return
	 */
	@CrudRequestMapping(method = RequestMethod.POST)
	public String save(T model,BindingResult result){
		crudService.save(model);
		return "redirect:{id}";
	}
	
	@CrudRequestMapping(value="{id}",method=RequestMethod.GET)
	public String info(@PathVariable String id ){
		System.out.println(id);
		return null;
	}
	
	/**
	 * update时页面跳转
	 * @param id
	 * @return
	 */
	@CrudRequestMapping(value="input/{id}",method = RequestMethod.GET)
	public String inputUpdate(@PathVariable String id){
		return null;
	}
	
	/**
	 * update时更新数据
	 * @param id
	 * @param model
	 * @return
	 */
	@CrudRequestMapping(value="{id}",method = RequestMethod.PUT)
	public String update(@PathVariable String id ,T model){
		return "redirect:{id}";
	}
	
	/**
	 * delete时删除数据
	 * @param id
	 * @return
	 */
	@CrudRequestMapping(value="{id}",method = RequestMethod.DELETE)
	public String delete(@PathVariable String id){
		return null;
	}
	
	@CrudRequestMapping("{id}")
	@ResponseBody
	public String example(@PathVariable String id ,T model){
		System.out.println(id);
		return id + model.toString();
	}
}
